package homework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author Linyh
 * @Description
 * @Date 2022/7/12 19:12
 **/

public class Book {
    private int bid;
    private String name;
    private float price;

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(int bid, String name, float price) {
        this.bid = bid;
        this.name = name;
        this.price = price;
    }

    public void sell() {
        System.out.println("这本书被卖了");
    }

    private void sell(float price, String person) {
        System.out.println("显示本书卖给了" + person + "，卖了" + price + "元");
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class c = Book.class;
        Constructor constructor = c.getConstructor(int.class,String.class,float.class);
        Book book = (Book) constructor.newInstance(1,"西游记",100f);
        Method method = constructor.getDeclaringClass().getDeclaredMethod("sell");
        Method method1 = constructor.getDeclaringClass().getDeclaredMethod("sell",float.class,String.class);
        method.invoke(book);
        method.setAccessible(true);
        method1.invoke(book,book.getPrice(),"张三");
        method1.setAccessible(true);

    }
}
